package 算法.leetcode.bytedance;

/**
 * @author lchenglong
 * @date 2022/5/6
 */
public class OfferString_II {

    public static void main(String[] args) {
        OfferString_II instance = new OfferString_II();
        String[] strs = new String[]{"","","flower"};
        String ans = instance.longestCommonPrefix(strs);
        System.out.println("ans= " +ans);
    }
    public String longestCommonPrefix(String[] strs) {
        // 边界条件
        if (strs==null)return "";
        if (strs.length==1)return strs[0];
        int idx = 0;
        StringBuilder sb = new StringBuilder();

        while (true){
            boolean flag = false;
            Character c = null;
            for (int i=0;i<strs.length;i++){
                String str = strs[i];
                if (idx>=str.length()||(c =(c==null? str.charAt(idx):c))!=str.charAt(idx)){
                    flag = true;
                    break;
                }
            }
            if (flag)break;
            sb.append(c);
            idx++;
        }
        return sb.toString();
    }
}
